package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.Question;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IQuestionStatisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.QuestionStatisVO;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 从mysql数据库中将数据同步到redis
 * redis数据预热
 */

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisService strategyStatisService;
    @Reference
    private IQuestionStatisService questionStatisService;
    @Reference
    private IQuestionService questionService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("==============vo数据初始化=========");
        //mysql -- redis -- vo

        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail strategyDetail : list) {
            if(strategyStatisService.hasStrategyStatisVO(strategyDetail.getId())){
                continue;
            }
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategyDetail,vo);//同属性可以拷贝
            //处理不同属性
            vo.setDestId(strategyDetail.getDest().getId());
            vo.setDestName(strategyDetail.getDest().getName());
            vo.setStrategyId(strategyDetail.getId());
            strategyStatisService.setStrategyStatis(vo);
        }

        List<Question> questions = questionService.list();
        for (Question question : questions) {
            if(questionStatisService.hasQuestionStatisVO(question.getId())){
                continue;
            }
            QuestionStatisVO vo = new QuestionStatisVO();
            BeanUtils.copyProperties(question,vo);//同属性可以拷贝
            //处理不同属性
            vo.setDestId(question.getDest().getId());
            vo.setDestName(question.getDest().getName());
            vo.setQuestionId(question.getId());
            questionStatisService.setQuestionStatis(vo);
        }
        System.out.println("==============vo数据完成==========");
        System.out.println("==============zset数据开始==========");
        for (StrategyDetail strategyDetail : list) {
            String zSetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            String voId = RedisKeys.STRATEGY_STATIS_VO.join(strategyDetail.getId().toString());
            if(strategyStatisService.isVoidExistZet(voId,zSetKey)){
                continue;
            }
            int score = strategyDetail.getViewnum()+strategyDetail.getReplynum();
            strategyStatisService.addScoreHot(strategyDetail.getId(),score);
        }
        //国内外排行初始化
        for (StrategyDetail strategyDetail : list) {
            String zSetKey = null;
            String voId = RedisKeys.STRATEGY_STATIS_VO.join(strategyDetail.getId().toString());
            if(strategyDetail.isIsabroad()){
                zSetKey = RedisKeys.STRATEGY_ABROAD_ZSET.getPrefix();
            }else {
                zSetKey = RedisKeys.STRATEGY_CHINA_ZSET.getPrefix();
            }
            if(strategyStatisService.isVoidExistZet(voId,zSetKey) ){
                continue;
            }

            int score = strategyDetail.getViewnum()+strategyDetail.getReplynum();
            strategyStatisService.addScoreAbroadAndChina(strategyDetail.getId(),score);


        }
        System.out.println("==============zset数据完成==========");
    }
}
